package me.lijf.mqlite.redis.stream;

import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.StreamInfo;
import org.springframework.data.redis.connection.stream.StreamOffset;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Component
public class RedisStreamUtil {
    @Resource
    private StringRedisTemplate template;

    public String createGroup(String key,String group){
        return template.opsForStream().createGroup(key,group);
    }

    public StreamInfo.XInfoConsumers queryCunsomers(String key,String group){
        return template.opsForStream().consumers(key,group);
    }

    public String write(String key, Map<String,Object> value){
        return template.opsForStream().add(key,value).getValue();
    }

    public List<MapRecord<String,Object,Object>> read(String key){
        return template.opsForStream().read(StreamOffset.fromStart(key));
    }

    public Long ack(String key,String group,String... ids){
        return template.opsForStream().acknowledge(key, group, ids);
    }

    public Long del(String key,String... ids){
        return template.opsForStream().delete(key,ids);
    }

    public boolean isExisted(String key){
        Boolean existed=template.hasKey(key);
        return existed==null?false:existed;
    }
}
